package com.maker.LMMS.controller;

import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.maker.LMMS.model.Emailcode;
import com.maker.LMMS.model.LeaveWord;
import com.maker.LMMS.model.Skills;
import com.maker.LMMS.model.Users;
import com.maker.LMMS.service.ShowImpl;
import com.maker.LMMS.service.UserImpl;
import com.maker.LMMS.service.Vcode;

/**
 * @author : zimu
 * @Date: 2018/8/2 22:30
 * @description :
 */
@Path
public class UserController {
    @PostRoute("login")
    @JSON
    public void login(@Param String email, @Param String password, Request request,
                      @Param String yzm , Response response){
        UserImpl user = new UserImpl();
        Vcode vcode = new Vcode();
        if (user.checkPass(email)==0) {
            response.json("{\"message\" : \"您的账号正在审核，暂时无法登录。\", \"flag\" : 4}");
            return;
        }
        if (user.checkPass(email)==-1) {
            response.json("{\"message\" : \"您的账号未通过审核，请重新注册。\", \"flag\" : 4}");
            return;
        }
        if (user.checkPass(email)==-2) {
            response.json("{\"message\" : \"该账号还未注册。\", \"flag\" : 4}");
            return;
        }
        if (user.checkVcode(request, yzm)) {
            if(user.login(email, password, request)){
                response.json("{\"message\" : \"登录成功！\", \"flag\" : 1}");
            }else {
                response.json("{\"message\" : \"用户名或密码错误\", \"flag\" : 2}");
            }
        }
        else
            response.json("{\"message\" : \"验证码输入错误！\", \"flag\" : 3}");

    }

    @GetRoute("logout")
    public void logout(Request request, Response response) {
        UserImpl user = new UserImpl();
        user.logout(request);
        response.redirect("/");
    }

    @GetRoute("/users/:id")
    public String showUsers(Request request, Response response) {
        Integer id = request.pathInt("id");
        Users users = new Users();
        users.setM_id(id);
        if (users.find()==null){
            response.html("<script>alert('该用户不存在！');window.location.href='/';</script>");
        }
        ShowImpl show = new ShowImpl();
        show.showinfo(request, id);
        show.showSkill(request, id);
        show.showLeaveword(request, id);
        return "/showinfo";
    }

    @PostRoute("/updatePassword")
    public void updatePassword(@Param String oldPassword, @Param String newPassword,Request request, Response response) {
        UserImpl user = new UserImpl();
        if(user.updatePassword(oldPassword, newPassword, request)){
            response.json("{\"tips\":\"密码修改成功！\",\"success\":\"1\"}");
        }else {
            response.json("{\"tips\":\"原密码不正确！\",\"success\":\"0\"}");
        }
    }


    @PostRoute("/dowanshan")
    @JSON
    public void dowanshan(@Param(name = "member") Users member, @Param(name = "skill") Skills skills, Request request, Response response){
        UserImpl user = new UserImpl();
        user.dowanshan(member, skills, request, response);
        response.json("{\"tips\":\"提交成功！\"}");
    }

    @PostRoute("signup")
    @JSON
    public void sign(Request request , @Param String realname, @Param String email, @Param String password,
                     @Param String vcode, @Param String ecode, Response response){
         UserImpl user = new UserImpl();
        if (!user.checkEmail(email)) {
            response.json("{\"state\" : 200, \"message\" : \"该邮箱已被注册！\" , \"flag\"" +
                    " : 0}");
            return;
        }
        if (!user.checkRegister()){
            response.json("{\"state\" : 200, \"message\" : \"抱歉！注册功能已关闭。\" , \"flag\"" +
                    " : 4}");
            return;
        }
        else if (user.checkVcode(request, vcode)) {
            if (user.checkEcode(request, ecode)) {
                Integer uid = user.signup(realname, email, password);
                response.json("{\"state\" : 200, \"message\" : \"恭喜你注册成功！请先完善本人信息，以方便审核。\" , \"flag\"" +
                        " : 1, \"uid\": "+uid+"}");
            } else {
                response.json("{\"state\" : 200, \"message\" : \"邮箱验证码输入有误，请重新获取。\" , \"flag\"" +
                        " : 2}");
            }
        } else {
            response.json("{\"state\" : 200, \"message\" : \"图片验证码输入有误，请重新获取。\" , \"flag\"" +
                    " : 3}");
        }
    }

    @PostRoute("/upload")
    @JSON
    public void upload(Request request, @Param String avatar_data, Response response){
        UserImpl user = new UserImpl();
        String flag = user.upload(request, avatar_data,response);
        String imgPath = flag+request.session().attribute("type");
        request.session().remove("type");
        if (flag!="false")
            response.json("{\"state\" : 200, \"result\" : \"/static/upload/img/"+imgPath+"\"}");
    }


    @GetRoute("/search")
    public String search1(Request request){
        String  key = String.valueOf(request.query("key", ""));
        System.out.println(key);
        if (key.equals("")) {
            return "search";
        }
        int pageNo = Integer.parseInt(request.query("pageNo", "1"));
        ShowImpl show = new ShowImpl();
        show.showResult(request, key, pageNo);

        return "searchResult";
    }

    @PostRoute("domessage")
    public void message(@Param String content,@Param int accepterId,@Param int memberId, Response response){
//        content = Trunhtml.trunhtml(content);
        UserImpl user = new UserImpl();
        user.domessage(content, memberId,accepterId);
        response.json("{\"tips\":\"留言成功！\"}");
    }
    @PostRoute("readmessage")
    public void readmessage(@Param int id,Response response){
        UserImpl user = new UserImpl();
        user.readmessage(id);
        response.json("{\"tips\":\"已读！\"}");
    }
    @PostRoute("deletemessage/:id")
    public void deletemessage(@PathParam int id,Response response){
        LeaveWord leaveWord = new LeaveWord();
        leaveWord.delete("l_id",id);
        response.json("{\"tips\":\"删除成功！\"}");
    }


    @PostRoute("dofeedback")
    public void dofeedback(@Param String content, @Param int memberId, Response response){
        UserImpl user = new UserImpl();
        user.feedback(content, memberId);
        response.json("{\"success\":\"true\"}");
    }

    @GetRoute("vcode")
    @JSON
    public void vcode(Request request,Response response){
        UserImpl user = new UserImpl();
        user.getVcode(request, response);
        response.json("{\"state\" : 200}");
    }

    @GetRoute("ecode")
    @JSON
    public void evode(Request request,@Param String email, @Param String vcode, Response response) {
        UserImpl user = new UserImpl();
        if (!user.checkEmail(email)) {
            response.json("{\"state\" : 200, \"message\" : \"邮箱已被注册！\" , \"flag\" : 3}");
            return;
        }
        if(user.checkVcode(request, vcode)){
            user.sendEcode(request, email);
            response.json("{\"state\" : 200, \"message\" : \"验证码发送成功，请注意查收！\" , \"flag\" : 1}");
        }else{
            response.json("{\"state\" : 200, \"message\" : \"图片验证码有误，请重新获取！\" , \"flag\" : 2}");
        }

    }

    @GetRoute("/checkEmail")
    @JSON
    public void checkEmail(Request request, Response response, @Param String email) {
        UserImpl user = new UserImpl();
        if (user.checkEmail(email)) {
            response.json("{\"statue\" : 200,\"flag\":1,\"message\" : \"邮箱可以使用。\"}");
        } else {
            response.json("{\"statue\" : 200,\"flag\":2, \"message\" : \"邮箱已被使用。\"}");
        }
    }

    @PostRoute("/verification")
    @JSON
    public void verification(Request request, @Param String email, @Param String vcode,@Param String domain, Response response) {
        UserImpl user = new UserImpl();
        if (user.checkEmail(email)) {
            response.json("{\"state\" : 200, \"message\" : \"邮箱未注册！\" , \"flag\" : 3}");
            return;
        }
        if(user.checkVcode(request, vcode)){
            user.sendLink(request, email,domain);
            response.json("{\"state\" : 200, \"message\" : \"改密链接已通过邮件发送至您的邮箱，请注意查收！\" , \"flag\" : 1}");
        }else{
            response.json("{\"state\" : 200, \"message\" : \"图片验证码有误，请重新获取！\" , \"flag\" : 2}");
        }
    }

    @PostRoute("/forgotPassword")
    public void changePassword(Request request,@Param String email,@Param String code, @Param String password, Response response) {
        UserImpl user = new UserImpl();
        if (user.checkCode(request, email, code)) {
            user.changePassword(request, email, password);
            Emailcode emailcode = new Emailcode();
            emailcode.delete("e_mail",email);
            response.json("{\"state\" : 200, \"message\" : \"密码重置成功！\"}");

        }
        else{
            response.json("{\"state\" : -1, \"message\" : \"皮这一下还开心吗？\"}");
        }

    }

}
